
package com.android.matmanual;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;


public class GravityTest extends BaseActivity implements SensorEventListener{
    Context mContext = null;
    public static int ScreenWidth = 0;
    WakeLock mWakeLock;  
	
	private TextView xTextView, yTextView, zTextView;
	private float mGX = 0;
	private float mGY = 0;
	private float mGZ = 0;
	private SensorManager mSensorMgr = null;    
	private Sensor mSensor = null; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
		WindowManager windowManager = getWindowManager();    
		Display display = windowManager.getDefaultDisplay(); 
		ScreenWidth = display.getWidth();
		Log.d("GravityTest", "############################# ScreenWidth: "+ScreenWidth);
		setContentView(R.layout.gravity);
		setTitle(R.string.gsensor_title);
		
		xTextView = (TextView)findViewById(R.id.textview_x);
		yTextView = (TextView)findViewById(R.id.textview_y);
		zTextView = (TextView)findViewById(R.id.textview_z);
		
		mSensorMgr = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);   
	    mSensor = mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);   	   
	    mSensorMgr.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);  
		
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); 
		mWakeLock.acquire(); 
		 
    }
    
	
	public void onAccuracyChanged(Sensor arg0, int arg1) {
	    // TODO Auto-generated method stub
	    
	}

	public void onSensorChanged(SensorEvent event) {
	    mGX = event.values[SensorManager.DATA_X];
	    mGY= event.values[SensorManager.DATA_Y];
	    mGZ = event.values[SensorManager.DATA_Z];
		
	//	Log.d("GravityTest", "############################# onSensorChanged mGX: "+mGX);
	//    Log.d("GravityTest", "############################# onSensorChanged mGY: "+mGY);
	//	Log.d("GravityTest", "############################# onSensorChanged mGZ: "+mGZ);
		
		xTextView.setText("X: " + String.valueOf(mGX));
		yTextView.setText("Y: " + String.valueOf(mGY));
		zTextView.setText("Z: " + String.valueOf(mGZ));
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if (TestControl.IsAuto())
			setResult(TestControl.FALSE_RESULT);
		mSensorMgr.unregisterListener(this); 
        finish();
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub		
		super.onDestroy();
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
}
		 